package com.ensibs.nuitinfo2013;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.ensibs.nuitinfo2013.model.Collection;
import com.ensibs.nuitinfo2013.model.Topic;
import com.ensibs.nuitinfo2013.model.Guide;

import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.JsonReader;
import android.util.Log;
import android.view.Menu;
import android.widget.*;
import android.view.View;
import android.text.*;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

public class GuideActivity extends Activity {

	EditText zoneSaisie =null; 
	EditText valBar=null;
	Button b=null;
	SeekBar s=null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.guide);
		
		//ExpandableListView elv=(ExpandableListView)findViewById(R.id.categorie);
		
		Spinner spin = (Spinner)findViewById(R.id.categorie);
		s=(SeekBar)findViewById(R.id.barPrix);
		b=(Button)findViewById(R.id.rechAch);
		zoneSaisie=(EditText)findViewById(R.id.champRech);
		valBar=(EditText)findViewById(R.id.valeurBar);
		List<String> categories = new ArrayList<String>();
		
		b.setOnClickListener(new View.OnClickListener(){
			public void onClick(View v){
				startActivity(new Intent (GuideActivity.this, CarouselActivity.class));
				
				/** data transfer  
				Intent mIntent = new Intent(GuideActivity.this, CarouselActivity.class); 
        		Bundle mBundle = new Bundle();
        		Guide guide = new Guide();
        	    guide.setChampRecherche(zoneSaisie.getText().toString());
        	    guide.setPrix(Integer.parseInt(valBar.getText().toString()));
        	    // guide.setSpinner(elv); 
        		mBundle.putSerializable("guide", guide);
        		mIntent.putExtras(mBundle);
        		startActivity(mIntent);
				**/
			}
		}); 
		
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		StrictMode.setThreadPolicy(policy); 
		
		JSONTokener tok = new JSONTokener(
				getPage("http://nuitinfo-ensibs-2013.alwaysdata.net/tags"));
		try {
			JSONArray array = (JSONArray)tok.nextValue();
			for (int i = 0; i < array.length(); i++) {
				JSONObject obj = array.getJSONObject(i);
				categories.add(obj.getString("title"));
			}
		}catch (JSONException e) {
			Log.e("API", e.getMessage());
		}
		
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, categories);


		// valBar=(EditText)findViewById(R.id.valeurBar);

		spin.setAdapter(adapter);

		s.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				EditText val = (EditText) findViewById(R.id.valeurBar);
				val.setText("" + progress + "");
			}

			public void onStopTrackingTouch(SeekBar seekBar) {

			}

			public void onStartTrackingTouch(SeekBar seekBar) {

			}
		});

	}

	private static String getPage(String url) {
		StringBuffer sb = new StringBuffer();
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					new URL(url).openConnection().getInputStream()));
			String line = null;
			while ((line = reader.readLine()) != null)
				sb.append(line + "\n");
			reader.close();
		} catch (Exception e) {
			System.out.println(e.toString());
		}
		return sb.toString();
	}

}